<template>
    <div style="margin: 10px 10px">
      <div style="margin-bottom: 5px">
        <el-input v-model="searchTeaName" style="width: 150px;margin-right: 5px"></el-input>
        <el-button type="primary" @click="searchTeaPer">搜索</el-button>
      </div>
      <div>
        <el-table :data="teaPers" border>
          <el-table-column
            prop="roleName"
            label="老师名称"
            align="center"
            width="300">
          </el-table-column>
          <el-table-column
            prop="roleUsername"
            label="老师电话"
            align="center"
            width="420">
          </el-table-column>
          <el-table-column
            prop="permissionName"
            label="权限"
            align="center"
            width="300">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="200">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="showEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%">
        <el-form
          :model="teaPermiss"
          label-width="80px">
          <el-form-item label="老师姓名">
            {{teaPermiss.roleName}}
          </el-form-item>
          <el-form-item label="老师电话">
            {{teaPermiss.roleUsername}}
          </el-form-item>
          <el-form-item label="权限">
            <el-select v-model="permissionId" placeholder="请选择">
              <el-option
                v-for="item in permissions"
                :key="item.permissionId"
                :label="item.permissionName"
                :value="item.permissionId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
      </el-dialog>
      <el-pagination
        background
        @current-change="currentChange"
        :current-page="currentPage"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
</template>

<script>
import {initTeaPer,editTeaPer,getTeaByName} from '../../api/admin/teaPer'
import {getPermission} from '../../api/admin/recStu'

export default {
  data() {
    return {
      permissionId: '',
      currentPage: 1,
      total: 100,
      dialogVisible: false,
      searchTeaName: '',
      teaPers: [{
        roleId: 0,
        roleName: '',
        roleUsername: '',
        permissionId: 0,
        permissionName: ''
      }],
      permissions: [{
        permissionId: 0,
        permissionName: ''
      }],
      teaPermiss: {
        roleId: 0,
        roleName: '',
        roleUsername: '',
        permissionId: 0,
        permissionName: ''
      }
    }
  },
  methods: {
    // 通过姓名搜索
    searchTeaPer() {
      if (this.searchTeaName === ''){
        this.currentChange(1)
      }else {
        getTeaByName(this.searchTeaName).then(response=>{
          console.log(response)
          this.teaPers = response.data.records
          this.total = response.data.total
        })
      }
    },
    // 展开编辑
    showEdit(row) {
      let old = JSON.stringify(row)
      this.teaPermiss = JSON.parse(old)
      this.dialogVisible = true
      this.permissionId = this.teaPermiss.permissionName
    },
    // 编辑
    update() {
      this.teaPermiss.permissionId = this.permissionId
      editTeaPer(this.teaPermiss).then(response=>{
        this.$message({
          message: response.msg,
          type: 'success'
        })
        this.currentChange(this.currentPage)
        this.dialogVisible = false;
      })
    },
    // 加载当前初始化页面
    currentChange(page){
      this.currentPage = page
      initTeaPer(this.currentPage).then(response=>{
        console.log(response)
        this.teaPers = response.data.records
        this.total = response.data.total
      })
    },
    // 获取权限列表
    getPer(){
      getPermission().then(response=>{
        this.permissions = response.data
      })
    }
  },
  created() {
    this.currentChange(1)
    this.getPer()
  }
}
</script>

<style scoped>

</style>
